﻿/*******************************************************************
** 文件名: Trace.h
** 版  权: (C) 深圳未名网络技术有限公司(664510732@qq.com)
** 创建人: DaiYuanzhi
** 日  期: 2021/03/28  22:05:01
** 版  本: 1.0
** 描  述:
** 应  用:

**************************** 修改记录 *******************************
** 修改人:
** 日  期:
** 描  述:
********************************************************************/

#ifndef __SERVER_LOG_ADAPTER_H__
#define __SERVER_LOG_ADAPTER_H__

#include "Common.h"
#include "Singleton.h"
//#include <iostream>

using std::string;
using namespace std;

#ifdef RKT_WIN32
#define GET_FILE_NAME(x) strrchr(x,'\\')?strrchr(x,'\\')+1:x
#else
#define GET_FILE_NAME(x) strrchr(x,'/')?strrchr(x,'/')+1:x
#endif

enum class EmTraceLevel
{
    TRACE_LEVEL_INFO,
    TRACE_LEVEL_TRACE,
    TRACE_LEVEL_WARNING,
    TRACE_LEVEL_ERROR,
    TRACE_LEVEL_DEBUG,
};


class CServerLogAdapter
{
public:
    CServerLogAdapter()
    {
        InitializeCriticalSection(&m_csLog);
    }

    ~CServerLogAdapter()
    {
        DeleteCriticalSection(&m_csLog);
    }

public:

    // 打印时间
    void GetCurTimeString(char* pszTimeStr, int nLen)
    {
        if (NULL == pszTimeStr || nLen <= 0)
        {
            return;
        }

        SYSTEMTIME stSysTime = {0};
        GetLocalTime(&stSysTime);

        sprintf_s(pszTimeStr, nLen, "%4d-%02d-%02d %02d:%02d:%02d", stSysTime.wYear, stSysTime.wMonth, stSysTime.wDay,
            stSysTime.wHour, stSysTime.wMinute, stSysTime.wSecond);

    }

    // 打印日志
    void PrintMessage(const char* pszFileName, const char* pszFuncName, int nLine, EmTraceLevel nTraceLevel, const char* szFormat, ...)
    {
        //cout << hex << this << endl;

        // 进入临界区
        ::EnterCriticalSection(&m_csLog);

        string strTraceNote;
        switch (nTraceLevel)
        {
        case EmTraceLevel::TRACE_LEVEL_INFO:
            strTraceNote = "INFO: ";
            break;
        case EmTraceLevel::TRACE_LEVEL_TRACE:
            strTraceNote = "TRACE: ";
            break;
        case EmTraceLevel::TRACE_LEVEL_WARNING:
            strTraceNote = "WARNING: ";
            break;
        case EmTraceLevel::TRACE_LEVEL_ERROR:
            strTraceNote = "ERROR: ";
            break;
        case EmTraceLevel::TRACE_LEVEL_DEBUG:
            strTraceNote = "DEBUG: ";
            break;
        default:
            strTraceNote = "UNKNOWN: ";
            break;
        }

        // 时间
        char szTimeStr[64] = {0};
        GetCurTimeString(szTimeStr, sizeof(szTimeStr));

        printf("[%s] <%s:%d %s> %s", szTimeStr, pszFileName, nLine, pszFuncName, strTraceNote.c_str());

        // 处理变长参数
        va_list arglist;
        va_start(arglist, szFormat);
        vprintf(szFormat, arglist);
        va_end(arglist);
        printf("\n");

        ::LeaveCriticalSection(&m_csLog);
    }


private:
    CRITICAL_SECTION m_csLog; // 用于Worker线程同步的互斥量
};

//!一般来说只会用到一个CServerLogAdapter类，所以实现成单件
typedef CSingleton<CServerLogAdapter> ServerLogSingleton;

#define TRACESVR(pszFormat, ...) (ServerLogSingleton::GetInstance()->PrintMessage(GET_FILE_NAME(__FILE__), __FUNCTION__, __LINE__, EmTraceLevel::TRACE_LEVEL_TRACE, pszFormat, __VA_ARGS__))
#define WARNINGSVR(pszFormat, ...) (ServerLogSingleton::GetInstance()->PrintMessage(GET_FILE_NAME(__FILE__), __FUNCTION__, __LINE__, EmTraceLevel::TRACE_LEVEL_WARNING, pszFormat, __VA_ARGS__))
#define ERRORSVR(pszFormat, ...) (ServerLogSingleton::GetInstance()->PrintMessage(GET_FILE_NAME(__FILE__), __FUNCTION__, __LINE__, EmTraceLevel::TRACE_LEVEL_ERROR, pszFormat, __VA_ARGS__))



#endif // __SERVER_LOG_ADAPTER_H__


